<!-- 弹框方式-ueditor -->
<template>
  <el-dialog
    customClass="customWidth"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="15">
          <el-form-item label="文档类型" prop="typeId">
            <el-select v-model="dataForm.typeId" filterable placeholder="请选择文档分类" size="medium" @change="onSelectType">
              <el-option v-for="item in documentCatList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="是否置顶" prop="showtop">
            <el-radio-group v-model="dataForm.showtop">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="关键词" prop="keyword">
            <el-input v-model="dataForm.keyword" placeholder="关键词"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="文档标题" prop="title">
            <el-input v-model="dataForm.title" placeholder="标题"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="文档图片" prop="picture">
            <div class="myhead">
              <el-tooltip content="点击上轮播图" placement="top">
                <el-image  style="width: 90px; height: 60px" :src="dataForm.picture" fit="contain" @click="uploadImageHandle()" alt=""/>
              </el-tooltip>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="文档内容" prop="content">
            <vue-ueditor-wrap
              v-model="dataForm.content"
              :config="myConfig"
              :destroy="true"
              @ready="ready"
              mode="observer"
              :observerDebounceTime="100"
              :observerOptions="{ attributes: true, characterData: true, childList: true, subtree: true }">
            </vue-ueditor-wrap>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗, 上传缩略图 -->
    <upload-image v-if="uploadImageVisible" ref="uploadImage" @refreshDataList="refreshUploadImage"></upload-image>
  </el-dialog>
</template>

<script>

  import VueUeditorWrap from 'vue-ueditor-wrap' // ES6 Module
  import UploadImage from './upload-image-common'
  export default {
    data () {
      return {
        visible: false,
        documentCatList: [
          {
            "id": 1,
            "name": "系统介绍",
          }, {
            "id": 2,
            "name": "关于我们",
          }, {
            "id": 3,
            "name": "系统教程",
          }, {
            "id": 4,
            "name": "隐私政策",
          }, {
            "id": 5,
            "name": "服务条款",
          }, {
            "id": 6,
            "name": "最新资讯",
          }, {
            "id": 7,
            "name": "系统帮助",
          }
        ],
        uploadImageVisible: false,
        dialogVisible: false,
        dataForm: {
          id: 0,
          typeId: '',
          typeName: '',
          title: '',
          keyword: '',
          content: '<h2><img src="http://img.baidu.com/hi/jx2/j_0003.gif"/>Vue + UEditor + v-model双向绑定</h2>',
          picture: 'http://file-cdn-theme.cykonhy.com/default/default.jpg',
          showtop: 1,
          clickcount: '250'
        },
        dataRule: {
          typeId: [
            { required: true, message: '类型id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          keyword: [
            { required: true, message: '关键词不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          picture: [
            { required: true, message: '图片URL不能为空', trigger: 'blur' }
          ],
          showtop: [
            { required: true, message: '是否置顶不能为空', trigger: 'blur' }
          ]
        },
        myConfig: {
          // 编辑器不自动被内容撑高
          autoHeightEnabled: false,
          // 初始容器高度
          initialFrameHeight: 300,
          // 初始容器宽度
          initialFrameWidth: '90%',
          // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
          serverUrl: window.SITE_CONFIG.baseUrl + '/upload/file',
          // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
          UEDITOR_HOME_URL: 'static/plugins/UEditor/',
        }
      }
    },
    components: {
      UploadImage,
      VueUeditorWrap
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/document/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.typeId = data.document.typeId
                this.dataForm.typeName = data.document.typeName
                this.dataForm.title = data.document.title
                this.dataForm.keyword = data.document.keyword
                this.dataForm.content = data.document.content
                this.dataForm.picture = data.document.picture
                this.dataForm.clickcount = data.document.clickcount
                this.dataForm.showtop = data.document.showtop
              }
            })
          }
        })
      },
      // 筛选类型
      onSelectType (val) {
        let documentType = null ;
        this.documentCatList.forEach((item, index) => {
          if (item.typeId === val){
            documentType = item
          }
        })
        this.dataForm.typeName = documentType.name
      },
      // 上传图片
      uploadImageHandle () {
        this.uploadImageVisible = true
        this.$nextTick(() => {
          this.$refs.uploadImage.init()
        })
      },
      refreshUploadImage (filePath) {
        if (filePath != ""){
          this.dataForm.picUrl = filePath
        }
        this.uploadImageVisible = false
      },
      ready (editorInstance) {
        console.log(`实例${editorInstance.key}已经初始化:`, editorInstance)
      },
      // 表单提交
      dataFormSubmit () {
        this.dataForm.content = this.ue.getContent()
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/document/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'typeId': this.dataForm.typeId,
                'typeName': this.dataForm.typeName,
                'title': this.dataForm.title,
                'keyword': this.dataForm.keyword,
                'content': this.dataForm.content,
                'picture': this.dataForm.picture,
                'clickcount': this.dataForm.clickcount,
                'showtop': this.dataForm.showtop,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss">
  .customWidth{
    width:70%;
  }
</style>
